package swing;



import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.MouseInputListener;
import javax.swing.table.DefaultTableModel;

import org.apache.ibatis.session.SqlSession;

import util.GetSqlSession;
import util.SwingElement;
import baseDao.WebsiteDao;
import entity.Website;

public class WebsiteSwing extends JDialog {
	JTable j111;//表格
	private int id;
	JTextField jf1;//网址
	JLabel jl1;//网址值
	String aa = null;
	JPanel j3;
	JButton jb1;//增加
	JButton jb2;//删除
	JButton jb3;//修改
	WebsiteSwing(JDialog aqa) {
		super(aqa, true);
		this.setTitle("网址库管理");
		this.setSize(920, 400);
		this.setResizable(false);
		this.setLocationRelativeTo(null);

		Container c = getContentPane();
		c.setLayout(null);

		j3 = SwingElement.getJP("");
		j3.setLayout(null);
		j3.setBounds(3, 3, 400, 300);
		j3.add(hehe());
		c.add(j3);
		
		jl1=SwingElement.getJL("网址值:");
		jl1.setBounds(420, 10, 60, 20);
		
		jf1=SwingElement.getJT("", 500, 10, 300, 20);
		c.add(jl1);
		c.add(jf1);
		
		jb1=SwingElement.getJB("添加");
		jb1.setBounds(420, 60, 80, 20);
		jb1.addActionListener(new MyLis());
		jb1.setActionCommand("添加");
		
		jb2=SwingElement.getJB("修改");
		jb2.setBounds(520, 60, 80, 20);
		jb2.addActionListener(new MyLis());
		jb2.setActionCommand("修改");
		
		jb3=SwingElement.getJB("删除");
		jb3.setBounds(620, 60, 80, 20);
		jb3.addActionListener(new MyLis());
		jb3.setActionCommand("删除");
		
		c.add(jb1);
		c.add(jb2);
		c.add(jb3);
		JButton qq1 = SwingElement.getJB("退出");
		qq1.addActionListener(new MyLis());
		qq1.setActionCommand("退出");
		
		c.add(qq1);
		
	}

	private class MyLis implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			SqlSession session = GetSqlSession.getSession();
			 WebsiteDao dao = session.getMapper(WebsiteDao.class);
			String str = e.getActionCommand();
			if (str == "删除") {
				if (id==0) {
					JOptionPane.showMessageDialog(null, "请选择");
				} else {
					Website w=new Website();
					 w.setId(id);
					 jf1.setText("");
					dao.delete(w);
					session.commit();
					 session.close();
					j3.removeAll();
					j3.repaint();
					j3.add(hehe());
					
				}
			}
			if (str == "修改") {
				if (id==0) {
					JOptionPane.showMessageDialog(null, "请选择");
				}else{
				//jdbc.heheda12(sql);//进行数据库操作
					 Website w=new Website();
					 w.setId(id);
					 w.setWebsite(jf1.getText());
					dao.update(w);
					session.commit();
					 session.close();
					j3.removeAll();
					j3.repaint();
					j3.add(hehe());
				}
			}
			if (str == "添加") {
				 Website w=new Website();
				 w.setWebsite(jf1.getText());
				 dao.add(w);
				 session.commit();
				 session.close();
				j3.removeAll();
				j3.repaint();
				j3.add(hehe());
			}
			id=0;
			jf1.setText("");
			// 弹窗
			// JOptionPane.showMessageDialog(null, "点我干嘛");
		}
	}

	private class mm implements MouseInputListener {

		@Override
		public void mouseClicked(java.awt.event.MouseEvent e) {
			int row = j111.getSelectedRow();
			// JOptionPane.showMessageDialog(null, row);
			String str1 = (String) j111.getValueAt(row, 1);
			jf1.setText(str1);
			id=(int) j111.getValueAt(row, 0);
			aa = str1;
		}

		@Override
		public void mousePressed(java.awt.event.MouseEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseEntered(java.awt.event.MouseEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseExited(java.awt.event.MouseEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseReleased(java.awt.event.MouseEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseDragged(java.awt.event.MouseEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseMoved(java.awt.event.MouseEvent e) {
			// TODO Auto-generated method stub

		}

	}
	public JPanel hehe(){
		JPanel jaja=new JPanel();
		jaja.setLayout(null);
		jaja.setBounds(3, 10, 380, 280);
		String[] a = { "编号","网址"};
		j111 = SwingElement.Table(a, 15, 25, 370, 260);
		j111.addMouseListener(new mm());
		DefaultTableModel dtm = (DefaultTableModel) j111.getModel();
		//链接数据库.heheda2(dtm);//查询
		 SqlSession session = GetSqlSession.getSession();
		 WebsiteDao dao = session.getMapper(WebsiteDao.class);
		 List<Website> list = dao.selectAll();
		 for (int i = 0; i < list.size(); i++) {
			 Vector v = new Vector();
			 v.add(list.get(i).getId());
			 v.add(list.get(i).getWebsite());
			 dtm.addRow(v);
		}
		 session.close();
		j111.getColumnModel().getColumn(0).setMaxWidth(120);// 获取模型的列，设置每一列的宽度
		j111.getColumnModel().getColumn(0).setMinWidth(80);// 获取模型的列，设置每一列的宽度
		j111.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

		JScrollPane jsp111 = new JScrollPane(j111,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);// 滚动条
		jsp111.setBounds(3, 25, 360, 240);
		jaja.add(jsp111);
		return jaja;
	}
}
